package com.example.tys.baseapplication.View.banner;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.tys.baseapplication.R;

/**
 * Created by tys on 2017/10/10.
 */

public class BannerView extends FrameLayout implements BannerCore.BannerViewOnClickListen {
    private BannerCore bannerCore;
    private LinearLayout dotLl;
    private int screenWidth;
    private BannerViewClickListen bannerViewClickListen;

    public BannerView(@NonNull Context context) {
        this(context, null);
    }

    public BannerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initBannerViewGroup();
        initDotLinearLayout();
        screenWidth = getResources().getDisplayMetrics().widthPixels;
    }

    private void initDotLinearLayout() {
        dotLl = new LinearLayout(getContext());
        dotLl.setOrientation(LinearLayout.HORIZONTAL);
        dotLl.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        dotLl.setLayoutParams(lp);
        dotLl.setBackgroundColor(Color.TRANSPARENT);
        dotLl.setAlpha(0.5f);
        addView(dotLl);
    }

    private void initBannerViewGroup() {
        bannerCore = new BannerCore(getContext()) {
            @Override
            void bannerImagePosition(int position) {
                setSelectedDot(position);
            }
        };
        bannerCore.setBannerViewClickListen(this);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        bannerCore.setLayoutParams(lp);
        addView(bannerCore);
    }

    private void setSelectedDot(int position) {
        int dotCount = dotLl.getChildCount();
        for (int i = 0; i < dotCount; i++) {
            ImageView dotView = (ImageView) dotLl.getChildAt(i);
            if (i == position) {
                dotView.setImageResource(R.drawable.black_circular);
            } else {
                dotView.setImageResource(R.drawable.gray_circular);
            }
        }
    }


    public void addBannerImage(int[] resId) {
        for (int id : resId) {
            addBannerImage(id);
            addDotImage();
        }
        setSelectedDot(0);
    }

    private void addDotImage() {
        ImageView iv = new ImageView(getContext());
        iv.setScaleType(ImageView.ScaleType.CENTER);
        iv.setImageResource(R.drawable.gray_circular);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(5, 5, 5, 5);
        iv.setLayoutParams(lp);
        dotLl.addView(iv);
    }

    private void addBannerImage(int id) {
        ImageView iv = new ImageView(getContext());
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setImageResource(id);
        iv.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, ViewGroup.LayoutParams.MATCH_PARENT));
        bannerCore.addView(iv);
    }

    @Override
    public void onClick(int index) {
        if (bannerViewClickListen != null)
            bannerViewClickListen.onBannerViewClick(index);
    }

    public interface BannerViewClickListen {
        void onBannerViewClick(int index);
    }

    public void setBannerViewClickListen(BannerViewClickListen bannerViewClickListen) {
        this.bannerViewClickListen = bannerViewClickListen;
    }

    public void cancelScan(){
        bannerCore.cancelImageScan();
    }
}
